package com.liusand.service.impl;

import cn.hutool.core.util.IdUtil;
import com.liusand.common.Result;
import com.liusand.common.UserThreadLocalUtils;
import com.liusand.dto.ShoppingCartDto;
import com.liusand.dto.SubShoppingCartDto;
import com.liusand.exception.ShoppingCartException;
import com.liusand.mapper.DishMapper;
import com.liusand.mapper.ShoppingCartMapper;
import com.liusand.pojo.Dish;
import com.liusand.pojo.ShoppingCart;
import com.liusand.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author :liusha
 * packageName :com.liusand.service.impl
 * fileName :ShoppingCartServiceImpl
 * creationTime :2022/12/1 21:12
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Override
    public List<ShoppingCart> list() {
        String id = UserThreadLocalUtils.getId();
        List<ShoppingCart> list = shoppingCartMapper.list(id);
        return list;
    }

    @Override
    public ShoppingCart joinShoppingCart(ShoppingCartDto shoppingCartDto) {
        if(shoppingCartDto == null){
            throw new ShoppingCartException("新增失败");
        }
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDto, shoppingCart);
        shoppingCart.setId(IdUtil.getSnowflakeNextIdStr());
        shoppingCart.setUserId(UserThreadLocalUtils.getId());
        shoppingCart.setNumber(1);
        ShoppingCart shoppingCartInDb = shoppingCartMapper.selectShoppingCart(shoppingCart);
        int result = 0;
        if(shoppingCartInDb == null) {
            result = shoppingCartMapper.insertDishOrSetmealToShoppingCart(shoppingCart);
        }else{
            shoppingCart.setNumber(shoppingCartInDb.getNumber() + 1);
            result = shoppingCartMapper.updateNumberInShoppingCart(shoppingCart);
        }
        shoppingCartInDb = shoppingCartMapper.selectShoppingCart(shoppingCart);
        return result == 1 ? shoppingCartInDb : null;
    }

    @Override
    public ShoppingCart subShoppingCart(SubShoppingCartDto subShoppingCartDto) {
        if(subShoppingCartDto == null){
            throw new ShoppingCartException("减少失败");
        }
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(subShoppingCartDto, shoppingCart);
        shoppingCart.setUserId(UserThreadLocalUtils.getId());
        ShoppingCart shoppingCartInDb = shoppingCartMapper.selectShoppingCart(shoppingCart);
        if(shoppingCartInDb == null){
            throw new ShoppingCartException("减少失败");
        }
        int result = 0;
        if(shoppingCartInDb.getNumber() == 1){
            result = shoppingCartMapper.deleteShoppingCart(shoppingCartInDb.getId());
            return result == 1 ? null : shoppingCartInDb;
        }
        shoppingCart.setNumber(shoppingCartInDb.getNumber() - 1);
        result = shoppingCartMapper.updateNumberInShoppingCart(shoppingCart);
        shoppingCartInDb = shoppingCartMapper.selectShoppingCart(shoppingCart);

        return result == 1 ? shoppingCartInDb : null;
    }

    @Override
    public boolean cleanShoppingCart() {
        shoppingCartMapper.deleteAllShoppingCartByUserId(UserThreadLocalUtils.getId());
        return true;
    }
}
